package databaseproject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 *
 * @author jesseday
 */
public class Song {
    private static Connection con;
    private static String username;
    private static boolean userIsAdmin;

    
    public static void doMenu(Connection nCon, String nUsername, boolean nUserIsAdmin)
    {
        con = nCon;
        username = nUsername;
        userIsAdmin = nUserIsAdmin;
        
        boolean done = false;

        while (!done)
        {
            Menu menu = new Menu();
            menu.setTitle("Songs");
            menu.addOption("List Songs");
            if (userIsAdmin)
            {
                menu.addOption("Add Song");
                
            }
            switch (menu.display())
            {
                case (1):
                    Menu listMenu = new Menu();
                    listMenu.addOptionsFromSQLQuery(con, "select sname from SONG");
                    int choice = listMenu.display();
                    displayDetail(listMenu.getOptionAt(choice));
                    break;
                
                case (2):
                    if (!userIsAdmin)
                    {
                        done = true;                        
                        break;       
                    }
                    Scanner input = new Scanner(System.in);
                    System.out.print("Song Name: ");
                    String sname = input.nextLine();
                    System.out.print("Song Length (minutes): ");
                    int min = input.nextInt();
                    System.out.print("            (seconds): ");
                    int sec = input.nextInt();
                    int n = 0;
                    String length = String.format("%02d:%02d:%02d", n, min, sec);
                    
                    try
                    {
                        PreparedStatement st = con.prepareStatement("insert into SONG values (?,'"+length+"')");
                        st.setString(1, sname);
                        st.executeUpdate();
                    }
                    catch (java.sql.SQLException e)
                    {
                        System.out.println("SQL failed in add song." + e);
                        
                    }
                    break;
                           
                
                default:
                    done = true;
                    break;
            }
        }
    }
    
    public static void displayDetail(String selection)
    {
        try
        {
            PreparedStatement stmt = con.prepareStatement("select * from SONG where sname=?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
            stmt.setString(1, selection);
            ResultSet rs = stmt.executeQuery();


            rs.first();
            
            System.out.printf("Song Name: %s\n",rs.getString("sname"));
            System.out.printf("Length: %s\n",rs.getTime("length").toString());

        }
        catch (java.sql.SQLException e)
        {
            return;
        }
        
    }
}
